<template>
  <el-container class="invoice-page panel-block">
    <el-header class="sa-header">
      <el-tabs class="sa-tabs" v-model="filterParams.data.status" @tab-change="getData(1)">
        <el-tab-pane
          v-for="sl in statusList"
          :key="sl"
          :label="sl.name"
          :name="sl.type"
        ></el-tab-pane>
      </el-tabs>
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">订单发票</span>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="loading">
      <el-table height="100%" class="sa-table" :data="table.data" row-key="id" stripe>
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column prop="id" label="ID" min-width="100" align="center"> </el-table-column>
        <el-table-column label="申请用户" min-width="200">
          <template #default="scope">
            <sa-user-profile :user="scope.row.user" :id="scope.row.user_id" />
          </template>
        </el-table-column>
        <el-table-column label="类型" min-width="110" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.type == 'person'">个人</el-tag>
            <el-tag v-if="scope.row.type == 'company'" type="warning">企/事业单位</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="抬头名称" min-width="240" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">
              {{ scope.row.name || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="联系电话" min-width="160" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">
              {{ scope.row.mobile || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="总金额" min-width="130" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">{{
              scope.row.amount ? scope.row.amount + '元' : '-'
            }}</div>
          </template>
        </el-table-column>
        <el-table-column label="实际开票金额" min-width="130" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">{{
              scope.row.invoice_amount ? scope.row.invoice_amount + '元' : '-'
            }}</div>
          </template>
        </el-table-column>
        <el-table-column label="纳税人识别号" min-width="200" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">
              {{ scope.row.tax_no || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="单位地址" min-width="300" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">
              {{ scope.row.address || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="开户行" min-width="300" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">
              {{ scope.row.bank_name || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单编号" min-width="250" align="center">
          <template #default="scope">
            <div class="sa-table-line-1">
              <el-button
                class="is-link"
                type="primary"
                @click="openOrderDetail(scope.row.order_id)"
                >{{ scope.row.order?.order_sn || '-' }}</el-button
              >
              <sa-svg
                class="copy sa-m-l-4 cursor-pointer"
                name="sa-copy"
                @click="useClip(scope.row.order?.order_sn)"
              ></sa-svg>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" min-width="180" align="center">
          <template #default="scope">
            {{ scope.row.create_time || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="处理时间" min-width="180" align="center">
          <template #default="scope">
            {{ scope.row.finish_time || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="120" align="center">
          <template #default="scope">
            <div
              class="sa-table-line-1"
              :class="
                scope.row.status == 'cancel'
                  ? 'sa-color--danger'
                  : scope.row.status == 'unpaid'
                  ? 'sa-color--info'
                  : scope.row.status == 'waiting'
                  ? 'sa-color--warning'
                  : 'sa-color--success'
              "
            >
              {{ scope.row.status_text || '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" min-width="100">
          <template #default="scope">
            <el-button
              v-if="scope.row.status == 'waiting'"
              v-auth="'shop.admin.order.invoice.confirm'"
              class="is-link"
              type="primary"
              @click="issue(scope.row)"
            >
              确认开具</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../order.service';
  import { ElMessageBox, ElMessage } from 'element-plus';
  import { useModal, usePagination } from '@/sheep/hooks';
  import { useSearch } from '@/sheep/components/sa-table/sa-search/useSearch';
  import { composeFilter } from '@/sheep/utils';
  import OrderDetail from '../order/detail.vue';
  import InvoiceEdit from './edit.vue';
  import { cloneDeep } from 'lodash';
  import useClip from '@/sheep/utils/clipboard.js';
  // 列表
  const table = reactive({
    data: [],
    order: '',
    sort: '',
    selected: [],
  });
  const { pageData } = usePagination();
  const loading = ref(true);
  const statusList = [
    {
      name: '全部',
      type: 'all',
    },
    {
      name: '已取消',
      type: 'cancel',
    },
    {
      name: '未支付',
      type: 'unpaid',
    },
    {
      name: '等待处理',
      type: 'waiting',
    },
    {
      name: '已开具',
      type: 'finish',
    },
  ];
  const filterParams = reactive({
    tools: {
      status: { label: '状态', value: 'all' },
      order: {
        type: 'tinputprepend',
        label: '订单信息',
        field: 'order',
        placeholder: '请输入查询内容',
        order: {
          field: 'order.order_sn',
          value: '',
        },
        options: [
          {
            label: '订单编号',
            value: 'order.order_sn',
          },
          {
            label: '订单ID',
            value: 'order_id',
          },
        ],
      },
      user: {
        type: 'tinputprepend',
        label: '用户信息',
        field: 'user',
        placeholder: '请输入查询内容',
        user: {
          field: 'user.nickname',
          value: '',
        },
        options: [
          {
            label: '用户昵称',
            value: 'user.nickname',
          },
          {
            label: '用户手机号',
            value: 'user.mobile',
          },
        ],
      },
      invoice: {
        type: 'tinputprepend',
        label: '发票信息',
        field: 'invoice',
        placeholder: '请输入查询内容',
        invoice: {
          field: 'name',
          value: '',
        },
        options: [
          {
            label: '发票抬头',
            value: 'name',
          },
          {
            label: '税号',
            value: 'tax_no',
          },
          {
            label: ' 联系方式',
            value: 'mobile',
          },
          {
            label: ' 公司地址',
            value: 'address',
          },
        ],
      },
      type: {
        type: 'tselect',
        label: '发票类型',
        field: 'type',
        value: '',
        options: {
          data: [
            {
              label: '全部',
              value: '',
            },
            {
              label: '个人',
              value: 'person',
            },
            {
              label: '企/事业单位',
              value: 'company',
            },
          ],
        },
      },
    },
    data: {
      order: { field: 'order.order_sn', value: '' },
      user: { field: 'user.nickname', value: '' },
      invoice: { field: 'name', value: '' },
      type: '',
      status: 'all',
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });
  // 获取
  async function getData(page) {
    loading.value = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);
    let search = composeFilter(tempSearch, {
      name: 'like',
      address: 'like',
      tax_no: 'like',
      'order.order_sn': 'like',
      'user.nickname': 'like',
      'user.mobile': 'like',
    });
    const { error, data } = await api.invoice.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      order: table.order,
      sort: table.sort,
      ...search,
    });
    if (error === 0) {
      table.data = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    loading.value = false;
  }
  //查看订单
  function openOrderDetail(id) {
    useModal(
      OrderDetail,
      {
        title: '订单详情',
        type: 'detail',
        id,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  function issue(row) {
    useModal(
      InvoiceEdit,
      {
        title: '编辑',
        type: 'edit',
        data: row,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  :deep() {
    .sa-user-popover .nickname {
      font-size: 14px !important;
    }
  }
</style>
